package com.fjd.service.impl;

import com.fjd.context.BaseContext;
import com.fjd.dto.AssessDTO;
import com.fjd.entity.*;
import com.fjd.mapper.*;
import com.fjd.service.AssessService;
import com.fjd.vo.AssessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class AssessServiceImpl implements AssessService {
    @Autowired
    private AssessMapper assessMapper;
    @Autowired
    private ShoppingCustomerInfoMapper shoppingCustomerInfoMapper;
    @Autowired
    private AssessImageMapper assessImageMapper;
    @Autowired
    private SalesDetailMapper salesDetailMapper;
    @Autowired
    private ShoppingProductMapper shoppingProductMapper;
    @Autowired
    private MerchandiseInfoMapper merchandiseInfoMapper;
    @Autowired
    private SalesOrderMapper salesOrderMapper;

    /**
     * 根据商品id获取评价列表
     * @param productId
     * @return
     */
    @Override
    public List<AssessVO> list(String productId) {
        List<Assess> assessList = assessMapper.getListByProductId(productId);
        return getAssessVOList(assessList);
    }

    @Override
    @Transactional
    public void add(AssessDTO assessDTO) {
        Assess assess = new Assess();
        BeanUtils.copyProperties(assessDTO, assess);
        assess.setCustomerId(String.valueOf(BaseContext.getCurrentId()));
        assess.setMerchandiseId(getMerchandiseId(assessDTO.getOrderId()));
        assess.setGenerateTime(LocalDateTime.now());
        assess.setId(UUID.randomUUID().toString().replace("-",""));
        //添加评价
        assessMapper.add(assess);
        //添加评价图片
        if(assessDTO.getImageList().size()!=0 && assessDTO.getImageList() != null){
            setAssessImageList(assess.getId(), assessDTO.getImageList());
        }
        //订单状态修改为已评价
        salesOrderMapper.updateStatus(assessDTO.getOrderId(), SalesOrder.REVIEWED);
    }

    public String getMerchandiseId(String orderId) {
        List<SalesDetail> salesDetailList = salesDetailMapper.getOrderDetail(orderId);
        ShoppingProduct shoppingProduct = shoppingProductMapper.getById(salesDetailList.get(0).getProductId());
        MerchandiseInfo merchandiseInfo = merchandiseInfoMapper.getById(shoppingProduct.getParentId());
        return merchandiseInfo.getMerchandiseId();
    }

    public void setAssessImageList(String assessId, List<String> imageList){
        assessImageMapper.insert(assessId, imageList);
    }

    public List<AssessVO> getAssessVOList(List<Assess> assessList){
        List<AssessVO> assessVOList = new ArrayList<>();
        for (Assess assess : assessList) {
            AssessVO assessVO = new AssessVO();
            BeanUtils.copyProperties(assess, assessVO);
            //获取用户名
            String username = shoppingCustomerInfoMapper.getUsernameById(assess.getCustomerId());
            assessVO.setCustomerName(username);
            //获取用户头像
            String avatar = shoppingCustomerInfoMapper.getAvatarById(assess.getCustomerId());
            assessVO.setAvatarUrl(avatar);
            //获取图片列表
            assessVO.setImageList(assessImageMapper.getByAssessId(assess.getId()));
            if (assessVO.getIsAnonymous()){
                assessVO.setAvatarUrl("https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0");
                assessVO.setCustomerName("匿名用户");
            }
            assessVOList.add(assessVO);
        }
        return assessVOList;
    }
}
